//
//  ComplaintBusinessCustomCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/22.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//



#import "MeatComplaintCustomCell.h"
#import "MeatComplaintModel.h"
#import "CustomImage.h"

#define MaxCount 200

@interface MeatComplaintCustomCell()<UITextViewDelegate>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *leftLineView;
@property (nonatomic, strong) CustomImage *oneImage;
@property (nonatomic, strong) CustomImage *twoImage;
@property (nonatomic, assign) long count;


@end


@implementation MeatComplaintCustomCell


- (void)setupUI{
    
    [self addShadowAndCircleCorner:self.layer corner:8];
    
    [self addSubview:self.textView];
    [self addSubview:self.oneImage];
    [self addSubview:self.twoImage];
    [self addSubview:self.placeHolderNumLab];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    _oneImage.mj_y = _twoImage.mj_y = self.textView.bottom + 21;
    _twoImage.mj_x = _oneImage.right + 10;
    
    _placeHolderNumLab.mj_y = self.height - 20 - _placeHolderNumLab.height;
    _placeHolderNumLab.mj_x = self.width - _placeHolderNumLab.width - 15;
    
}

#pragma mark - delegate

- (void)textViewDidBeginEditing:(UITextView *)textView{
    _placeHolderLab.hidden = YES;
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //如果是删除减少字数，都返回允许修改
    if ([text isEqualToString:@""]) {
        return YES;
    }
    
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    if (range.location>= MaxCount){
        return NO;
    }else{
        return YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    
    [self updateRemainCount];
}

- (void)updateRemainCount {
    //long count = MAX(0, (int)(MaxCount - self.textView.text.length));
    _count = self.textView.text.length;
    if (_count <=0) {
        _placeHolderLab.hidden = NO;
    }else{
        _placeHolderLab.hidden = YES;
    }
    self.placeHolderNumLab.text = [NSString stringWithFormat:@"%ld/200",_count];
}


#pragma mark - setter and getter

- (void)setModel:(MeatComplaintModel *)model
{
    _model = model;
    //更新图片
    {
        if (_model.photos.count==1) {
            
            _oneImage.image = model.photos[0];
            _oneImage.hidden = NO;
            _oneImage.cancelLab.hidden = NO;
            
            _twoImage.hidden = NO;
            _twoImage.cancelLab.hidden = YES;
        }else if (_model.photos.count == 2){
            
            _oneImage.image = model.photos[0];
            _oneImage.hidden = NO;
            _oneImage.cancelLab.hidden = NO;
            
            _twoImage.image = model.photos[1];
            _twoImage.hidden = NO;
            _twoImage.cancelLab.hidden = NO;
        }else{
            
            _oneImage.hidden = NO;
            _oneImage.image = [UIImage imageNamed:@"meat_complaint_add"];
            _oneImage.cancelLab.hidden = YES;
            
            _twoImage.hidden = YES;
            _twoImage.cancelLab.hidden = YES;
        }
    }
    
    //更新反馈问题
    {
        self.textView.text = model.content;
        _count = [model.content length];
        [self updateRemainCount];
    }
}


- (UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 15, KScreenWidth - 30, 120)];
        _textView.font = mFont(13);
        _textView.textColor = UIColorFromRGB(0x3333333);
        _textView.editable = YES;
        _textView.userInteractionEnabled = YES; ///
        _textView.delegate = self;
        _textView.scrollEnabled = YES;//滑动
        _textView.returnKeyType = UIReturnKeyDone;//返回键类型
        _textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
        
        [_textView addSubview:self.placeHolderLab];
        _placeHolderLab.mj_x = 5;
        _placeHolderLab.mj_y = 5;
    }
    return _textView;
}

- (UILabel *)placeHolderLab{
    
    if (!_placeHolderLab) {
        _placeHolderLab = [[UILabel alloc] init];
        _placeHolderLab.text = @"请输入描述信息";
        _placeHolderLab.textColor = UIColorFromRGB(0x999999);
        _placeHolderLab.font = mFont(13);
        _placeHolderLab.textAlignment = NSTextAlignmentLeft;
        _placeHolderLab.size = CGSizeMake(200, 20);
    }
    return _placeHolderLab;
}

- (UILabel *)placeHolderNumLab{
    if (!_placeHolderNumLab) {
        _placeHolderNumLab = [[UILabel alloc] init];
        _placeHolderNumLab.text = @"0/200";
        _placeHolderNumLab.textAlignment = NSTextAlignmentCenter;
        _placeHolderNumLab.textColor = UIColorFromRGB(0xCCCCCC);
        _placeHolderNumLab.font = mFont(13);
        _placeHolderNumLab.size = CGSizeMake(100, 20);
        _placeHolderNumLab.textAlignment = NSTextAlignmentRight;
    }
    return _placeHolderNumLab;
}

- (CustomImage *)oneImage{
    if (!_oneImage) {
        _oneImage = [[CustomImage alloc] init];
        _oneImage.size = CGSizeMake(74, 74);
        _oneImage.mj_x = 15;

        
        kWeakSelf(self);
        _oneImage.addImageBlock = ^{
            
            if (weakself.dealPhotoBlock) {
                weakself.dealPhotoBlock(0, DealPhotoOperation_Add);
            }
        };
        
        _oneImage.cancelImageBlock = ^{
            if (weakself.dealPhotoBlock) {
                weakself.dealPhotoBlock(0, DealPhotoOperation_Remove);
            }
        };
    }
    return _oneImage;
}


- (CustomImage *)twoImage{
    if (!_twoImage) {
        _twoImage = [[CustomImage alloc] init];
        _twoImage.size = CGSizeMake(74, 74);
        _twoImage.hidden = YES;

        
        kWeakSelf(self);
        _twoImage.addImageBlock = ^{
            if (weakself.dealPhotoBlock) {
                weakself.dealPhotoBlock(1, DealPhotoOperation_Add);
            }
        };
        
        _twoImage.cancelImageBlock = ^{

            if (weakself.dealPhotoBlock) {
                weakself.dealPhotoBlock(1, DealPhotoOperation_Remove);
            }
        };
    }
    return _twoImage;
}


@end
